import asyncio
from aiogram import Bot, Dispatcher
from aiogram.filters import CommandStart, Command, CommandObject
from aiogram.types import Message
from aiogram.types.input_file import FSInputFile
from aiogram.utils.media_group import MediaGroupBuilder
from aiogram.enums import ParseMode
import logging
from utils import get_and_save_document
import sys
import os
from dotenv import load_dotenv

load_dotenv()

dp = Dispatcher()

TOKEN = os.getenv("TOKEN_T")


async def main():
    bot = Bot(token=TOKEN)
    await dp.start_polling(bot)

@dp.message(CommandStart())
async def start(message: Message):
    await message.answer("Для того что бы начать вам необходимо прописать команду /generate и перечислить программы\
        \n\nПример:\n```\n/generate Word, PyCharm, AutoCad, Docker\n```", 
                         parse_mode=ParseMode.MARKDOWN_V2)

@dp.message(Command("generate"))
async def generate(message: Message, command: CommandObject):
    applications = command.args.split(",")
    media_group = MediaGroupBuilder()
    if len(applications) > 0:
        message = await message.answer("Ожидайте генерации...")
        document_path = await get_and_save_document(chat_id=message.chat.id, applications=applications)
        media_group.add_document(media=FSInputFile(document_path))
        await message.reply_media_group(media_group.build())
    else:
        await message.answer("Вы не ввели никаких программ")



if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    asyncio.run(main())
